<template>
  <div class="issue">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="">
        <el-input v-model="formInline.question" placeholder="请输入问题" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleLsh"
        >查找</el-button>
        <el-button
          type="primary"
          icon="el-icon-edit"
          @click="handleAdd(1)"
        >添加</el-button>
        <el-button
          type="primary"
          icon="el-icon-download"
          @click="handleDaochu"
        >导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      :data="tableData"
      style="width: 100%"
      :default-sort="{ prop: 'id', order: 'descending' }"
    >
      <el-table-column prop="id" label="问题ID" sortable width="100px" />
      <el-table-column prop="question" label="问题内容" />
      <el-table-column prop="answer" label="问题回复" width="900px" />
      <el-table-column prop="" label="操作" sortable>
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleDE(scope.row, 2)"
          >编辑</el-button>
          <el-button
            type="danger"
            @click="handleDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="obj.currentPage"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :page-size="obj.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="obj.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 模态框 -->
    <el-dialog :visible.sync="dialogVisible" width="40%">
      <p v-if="obj.val == '1'">创建</p>
      <p v-else-if="obj.val == '2'">编辑</p>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="问题" prop="question">
          <el-input v-model="ruleForm.question" />
        </el-form-item>
        <el-form-item label="回复" prop="answer">
          <el-input v-model="ruleForm.answer" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          v-if="obj.val == '1'"
          type="primary"
          @click="submitForm('ruleForm')"
        >确 定</el-button>
        <el-button
          v-else-if="obj.val == '2'"
          type="primary"
          @click="handleQ"
        >保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// 导入插件
import * as XLSX from 'xlsx'
import { saveAs } from 'file-saver'
import { listIssue, deleteIssue, createIssue, updateIssue } from '@/api/issue'
export default {
  data() {
    return {
      dialogVisible: false,
      formInline: {
        question: ''
      },
      tableData: [],
      row: '',
      obj: {
        currentPage: 1,
        pageSize: 20,
        total: null,
        val: null
      },
      ruleForm: {
        question: '',
        answer: ''
      },
      rules: {
        question: [{ required: true, message: '请输入', trigger: 'blur' }],
        answer: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.handleLsh()
  },
  methods: {
    // 调接口
    handleLsh() {
      const query = {
        page: this.obj.currentPage,
        limit: this.obj.pageSize,
        sort: 'add_time',
        order: 'desc',
        question: this.formInline.question
      }
      listIssue(query).then((response) => {
        console.log(response, '1212')
        this.tableData = response.data.data.list
        this.obj.total = response.data.data.total
      })
    },
    // 添加
    handleAdd(val) {
      this.dialogVisible = true
      this.obj.val = val
    },
    // 模态框确定按钮
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = {
            answer: this.ruleForm.answer,
            question: this.ruleForm.question
          }
          createIssue(data).then((response) => {
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            this.handleLsh()
            this.dialogVisible = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 编辑
    handleDE(row, val) {
      this.dialogVisible = true
      this.obj.val = val((this.ruleForm.question = row.question))(
        (this.ruleForm.answer = row.answer)
      )
      this.row = row
    },
    handleQ() {
      (this.row.question = this.ruleForm.question)(
        (this.row.answer = this.ruleForm.answer)
      )
      const data = this.row
      updateIssue(data).then((response) => {
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
        this.handleLsh()
        this.dialogVisible = false
      })
    },
    handleDaochu() {
      // 创建一个workbook
      const workbook = XLSX.utils.book_new() // 导出数据
      const worksheet = XLSX.utils.json_to_sheet(this.tableData)
      XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet 1') // 将workbook转为二进制数据

      const excelData = XLSX.write(workbook, {
        type: 'array',
        bookType: 'xlsx'
      }) // 创建blob对象并保存excel文件
      const blob = new Blob([excelData], { type: 'application/octet-stream' }) // 设置导出文件名字
      saveAs(blob, 'data.xlsx')
    },
    // 删除
    handleDel(row) {
      deleteIssue(row).then((response) => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.handleLsh()
      })
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.obj.pageSize = val
      this.handleLsh()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.obj.currentPage = val
      this.handleLSh()
    }
  }
}
</script>
<style lang="scss" scoped>
.issue ::v-deep {
  margin: 20px;
  .el-table {
    .el-table__cell {
      text-align: center;
    }
  }
  .el-pagination {
    margin: 40px 10px;
  }
  .el-dialog {
    p {
      position: absolute;
      top: 10px;
      left: 20px;
      font-size: 16px;
      font-weight: 600;
      color: #000;
    }
    .el-input {
      width: 300px;
    }
    .el-textarea__inner {
      width: 300px;
    }
  }
}
</style>

